/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iti.jets.gpts.service;

import com.iti.jets.gpts.pojo.AttributeType;
import com.iti.jets.gpts.pojo.TranServiceType;
import com.iti.jets.gpts.pojo.TranServiceTypeAttribute;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import java.util.ArrayList;
import java.util.List;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 * Jersey REST client generated for REST
 * resource:TransportationServiceTypeService [/transervicetype]<br>
 * USAGE:
 * <pre>
 *        ViewTransServiceTypeAtt client = new ViewTransServiceTypeAtt();
 *        Object response = client.XXX(...);
 *        // do whatever with response
 *        client.close();
 * </pre>
 *
 * @author noha
 */
public class ViewTransServiceTypeAtt {

    private WebResource webResource;
    private Client client;
    private static final String BASE_URI = "http://localhost:48651/GPTRP/rest";

    public ViewTransServiceTypeAtt() {
        com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
        client = Client.create(config);
        webResource = client.resource(BASE_URI).path("transervicetype");
    }

    public List<TranServiceTypeAttribute> getTranServiceTypeIndex(int id) throws UniformInterfaceException, JSONException {
        System.out.println("da5alllll alfunction");
        WebResource resource = webResource;
        resource = resource.path("getalltranservicetype/" + id);
        List<AttributeType> AttTypeArr = new ArrayList<AttributeType>();
        List<TranServiceTypeAttribute> TSTAArr = new ArrayList<TranServiceTypeAttribute>();
        JSONArray array = resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(JSONArray.class);
        JSONObject jSONObject;
        try {
            if (array.length() <= 0) {
                return TSTAArr;
            } else {
                jSONObject = array.getJSONObject(0);
                TranServiceType tranServiceTypeObject = new TranServiceType();
                int size = jSONObject.getInt("size");
                System.out.println("si" + size);
                for (int l = 0; l < size; l++) {
                    TranServiceTypeAttribute tranServiceTypeAttributeObject = new TranServiceTypeAttribute();
                    JSONObject tstA = jSONObject.getJSONObject("attributetype" + l);
                    tranServiceTypeAttributeObject.setValue(tstA.getString("attributetypevalue" + l));
                    System.out.println("da5alllllllllllllllllllllllahmed");
                    // tranServiceTypeAttributeObject.getAttributeType().setName(tstA.getString("attributetypename" + l));
                    System.out.println("5argggggggg ahmed");
                    AttributeType at = new AttributeType();
                    at.setName(tstA.getString("attributetypename" + l));
                    at.setId(tstA.getInt("attributetypeid" + l));
                    at.setIsNumber(tstA.getBoolean("attributetypeisnumber" + l));
                    tranServiceTypeAttributeObject.setAttributeType(at);

                    System.out.println(tranServiceTypeAttributeObject.getAttributeType().getId() + tranServiceTypeAttributeObject.getAttributeType().getName() + tranServiceTypeAttributeObject.getValue());
                // AttributeType at= tranServiceTypeAttributeObject.getAttributeType();
                    // at.setName(tstA.getString("attributetypename" + l));
                    System.out.println("valueeeeeee" + tstA.getString("attributetypevalue" + l) + "nameeeeee" + tstA.getString("attributetypename" + l));
                    AttTypeArr.add(at);
                    TSTAArr.add(tranServiceTypeAttributeObject);
                }
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
        return TSTAArr;
        // return resource.accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(responseType);
    }

    public void addTranServiceType(JSONArray TSTAArr) throws UniformInterfaceException, JSONException {
        WebResource resource = webResource;

        resource = resource.path("/add");
        ClientResponse response = resource.type("application/json").post(ClientResponse.class, TSTAArr);
        String output = response.getEntity(String.class);
        System.out.println("outttttttttttttt" + output);
        //JSONArray array = resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(JSONArray.class);

        // return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
    }

    public List<TranServiceType> getTranServiceTypes() throws UniformInterfaceException, JSONException {
        WebResource resource = webResource;
        resource = resource.path("/viewtranservicetype");
        JSONArray jsonArray = resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(JSONArray.class);
        List<TranServiceType> TransTypeArr = new ArrayList<TranServiceType>();
        System.out.println("jsonArray.length()" + jsonArray.length());
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jSONObject = jsonArray.getJSONObject(i);
            JSONObject transType = jSONObject.getJSONObject("trantype" + i);
            TranServiceType TSTObject = new TranServiceType();
            System.out.println("iddddddddd" + transType.getInt("trantypeid" + i));
            TSTObject.setId(transType.getInt("trantypeid" + i));
            System.out.println("namee" + transType.getString("trantypename" + i));
            TSTObject.setName(transType.getString("trantypename" + i));
            TransTypeArr.add(TSTObject);
        }
        return TransTypeArr;
    }

   public String addTranServiceTypeObject(int tranid,int attid,String attValue)
   {
      WebResource resource = webResource;
        resource = resource.path("/addTSTObject");
        String x = resource.path("/" + tranid + "/" + attid + "/" + attValue).accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
        return x;
   }
    public String UpdateTransSerTypeAtt(int oldid, int tranid, String name, String value) {
        WebResource resource = webResource;
        resource = resource.path("/update");
        String x = resource.path("/" + oldid + "/" + tranid + "/" + name + "/" + value).accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
        return x;
    }

    public void UpdateTransSerTypeAtt1(JSONArray TSTAArr) throws UniformInterfaceException, JSONException {
        WebResource resource = webResource;

        resource = resource.path("/updatee");
        ClientResponse response = resource.type("application/json").post(ClientResponse.class, TSTAArr);
        String output = response.getEntity(String.class);
        System.out.println("outttttttttttttt" + output);

    }

    public int getAttributeID(String name) {
        WebResource resource = webResource;
        resource = resource.path("/getattid");
        int num = resource.path("/" + name).accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(Integer.class);
        return num;
    }

    public String getAttributeName(int attid) {
        WebResource resource = webResource;
        resource = resource.path("/getattname");
        String name = resource.path("/" + attid).accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
        return name;
    }

    public String setTransportationTypeName(int tranid, String name) {
        WebResource resource = webResource;
        resource = resource.path("/editTransTypeName");
        String x = resource.path("/" + tranid + "/" + name).accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
        return x;
    }

    public void removeTS(int id) {
        WebResource resource = webResource;
        resource = resource.path("/removets");
       resource.path("/" + id).get(String.class);
       
    }
     public void removeTST(int attid,int tranid) {
        WebResource resource = webResource;
        resource = resource.path("/removetst");
       resource.path("/" + attid+"/"+tranid).get(String.class);
       
    }
    public void close() {
        client.destroy();
    }

}
